// subpkg_worker/bloodPressure_week/bloodPressure_week.js
var wxCharts = require('../../utils/wxcharts.js');
var lineChart = null;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date:'最近一周',
    max_date:new Date().getTime(),
    min_date:new Date(2023,8,1).getTime(),
    show:false,
    startTime:'',
    endTime:'',
    elderlyId:'',
    width:'',
    x_data:[],
    y_data:[],
    weight_x_data:[],
    weight_y_data:[],
    range : [40,120]
  },
  onDisplay() {
    this.setData({ show: true });
  },
  onClose() {
    this.setData({ show: false });
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getMonth() + 1}/${date.getDate()}`;
  },
  onConfirm(event) {
    
    const [start, end] = event.detail;
    var year = start.getFullYear()
    var month = start.getMonth() + 1 <10?'0'+(start.getMonth() + 1):start.getMonth() + 1
    var day = start.getDate()
    var year1 = end.getFullYear()
    var month1 = end.getMonth() + 1 <10?'0'+(end.getMonth() + 1):end.getMonth() + 1
    var day1 = end.getDate()
    var startTime = `${year}-${month}-${day} 00:00`
    var endTime = `${year1}-${month1}-${day1} 23:59`
    this.setData({
      show: false,
      startTime:startTime,
      endTime:endTime,
      date:`${this.formatDate(start)}-${this.formatDate(end)}`
    });
    this.query(startTime,endTime)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var windowWidth = wx.getSystemInfoSync().windowWidth/750 * 690
    this.setData({
      elderlyId:options.elderId,
      width:windowWidth
    })
    var date = new Date()
    var year = date.getFullYear()
    var month = date.getMonth()+1<10?'0'+(date.getMonth()+1):date.getMonth()+1
    var day = date.getDate()
    var endTime = `${year}-${month}-${day} 23:59`
    var date1 = new Date(date.getTime()-6*24*60*60*1000)
    var year1 = date1.getFullYear()
    var month1 = date1.getMonth()+1<10?'0'+(date1.getMonth()+1):date1.getMonth()+1
    var day1 = date1.getDate()
    var startTime = `${year1}-${month1}-${day1} 00:00`
    console.log(startTime)
    console.log(endTime)
    this.setData({
      startTime:startTime,
      endTime:endTime
    })
    this.query(startTime,endTime)
  

    var x_data=["8/13", "8/14", "8/15","8/16", "8/17", "8/18", "8/19"]
    var y_data= ["80", "79", "79", "79", "79", "79", "79"]
    //绘制折线图
    

    // this.getHealth()
    // this.getHealthList()

  },
  query(startTime,endTime){
    wx.request({
      url: 'https://zhixiao.site:8080/health/bodyWeight',
      data:{
        startTime:startTime,
        endTime:endTime,
        elderlyId:this.data.elderlyId
      },
     
      success:(res)=>{
        console.log(res)
        var weight_x_data = []
        var weight_y_data = []
        var weight = res.data.data
        for(var i in weight){
          weight_x_data[i] = weight[i].createdTime.slice(5,10)+'/'+weight[i].createdTime.slice(11,16)
          weight_y_data[i] = weight[i].weight
        }
        this.setData({
          weight_x_data:weight_x_data,
          weight_y_data:weight_y_data
        })
      },
      complete:(res)=>{
          this.OnWxChart(this.data.weight_x_data,this.data.weight_y_data,'体重',this.data.range)
      }
    })
  },

   //图表点击事件
 touchcanvas:function(e){
  lineChart.showToolTip(e, {
    format: function (item, category) {
      return category + ' ' + item.name + ':' + item.data
    }
  });
},
 
//折线图绘制方法
OnWxChart:function(x_data,y_data,name,range){
  
  var windowWidth = '', windowHeight='';    //定义宽高
  try {
    var res = wx.getSystemInfoSync();    //试图获取屏幕宽高数据
    windowWidth = res.windowWidth / 750 * 690;   //以设计图750为主进行比例算换
    windowHeight = res.windowWidth / 750 * 690    //以设计图750为主进行比例算换
    console.log(windowWidth)
    if(this.data.weight_x_data.length>5){
      console.log(this.data.weight_x_data.length)
      windowWidth = this.data.weight_x_data.length*70
      this.setData({
        width:windowWidth+40
      })
    }
  } catch (e) {
    console.error('getSystemInfoSync failed!');   //如果获取失败
  }
  lineChart = new wxCharts({
    canvasId: 'lineCanvas',     //输入wxml中canvas的id
    type: 'line',     
    categories:x_data,    //模拟的x轴横坐标参数
    animation: true,  //是否开启动画
   
    series: [{
      name: name,
      data: y_data,
      format: function (val, name) {
        return val ;
      }
    }
    ],
    xAxis: {   //是否隐藏x轴分割线
      disableGrid: true,
      fontColor:'black',
      
    },
    yAxis: {      //y轴数据
      title: '',  //标题
      format: function (val) {  //返回数值
        return val.toFixed(2);
      },
      min: range[0],   //最小值
      max: range[1],
      
      gridColor: '#D8D8D8',
      fontColor:'black'
      
    },
    width: windowWidth*1.1,  //图表展示内容宽度
    height: windowHeight,  //图表展示内容高度
    dataLabel: true,  //是否在图表上直接显示数据
    dataPointShape: true, //是否在图标上显示数据点标志
    extra: {
      lineStyle: 'Broken'  //曲线
    },
  });
},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})